{% extends "projects/base.html" %}

{% load zwitschern %}
{% load i18n %}
{% load avatar_tags %}
{% load wiki %}
{% load wikiurl %}
{% load threadedcommentstags %}
{% load pagination_tags %}
{% load shorttimesince_tag %}
{% load extra_tagging_tags %}
{% load project_tags %}

{% block head_title %}{{ project.name }}{% endblock %}

{% block body %}

    {% if user.is_authenticated %}
        <div class="right_panel">
            <div class="members">
                <h2>{% trans "Members" %}</h2>
                <table width="100%">
                    {% for member in project.members.all %}
                        {% if forloop.counter0|divisibleby:"3" %}<tr>{% endif %}
                        <td class="{% if member.away %}away{% else %}active{% endif %}-member">
                            <div class="avatar">{% avatar member.user 40 %}</div>
                            <div class="details"><a href="{% url profiles.views.profile member.user.username %}" title="{{ member.user.username }}">{{ member.user.username }}</a></div>
                        </td>
                        {% if forloop.counter0|add:"1"|divisibleby:"3" %}</tr>{% endif %}
                    {% endfor %}
                    {% if project.members.all|length|divisibleby:"3" %}{% else %}</tr>{% endif %}
                </table>
            </div>
            <p><a href="{% url project_members_status project.slug %}">Members' Status</a></p>
        </div>
    {% endif %}

    <div style="width: 550px;">

        <h1>{% trans "Project" %} {{ project.name }}</h1>

        <p>
            Slug: <tt>{{ project.slug }}</tt><br />
            Creator: <a href="{% url profiles.views.profile project.creator.username %}">{{ project.creator }}</a><br />
            Created: {{ project.created|date }}
            <br />
            {% show_tags_for project %}
        </p>

        <p>{{ project.description }}</p>

        {% ifequal user project.creator %}
            
            <div class="form-toggle">
                <p><span id="edit-project-toggle">{% trans "Edit details" %}</span></p>
                <form id="edit-project" method="POST" action="">
                    <table>
                        {{ project_form }}
                        <tr><td></td><td><input type="hidden" name="action" value="update" /><input type="submit" value="{% trans 'update' %}"/></td></tr>
                    </table>
                </form>
            </div>
            
        {% endifequal %}

        {% if user.is_authenticated %}
            <div>
                {% if are_member %}
                    {% ifequal user project.creator %}
                        <p>You are the creator of this project.</p>
                        
                        <div class="form-toggle">
                            <p><span id="add-member-toggle">{% trans "Add new member" %}</a></p>
                            <form id="add-member" method="POST" action="">
                                <table>
                                    {{ adduser_form }}
                                    <tr><td></td><td><input type="hidden" name="action" value="add" /><input type="submit" value="{% trans 'add' %}"/></td></tr>
                                </table>
                            </form>
                        </div>
                        
                        <p><img src="/site_media/delete.png" /> <a href="#" onclick="$('#delete_project_form').toggle(); return false;">{% trans "Delete project" %}</a><p>
                         <form class="delete_form" id="delete_project_form" action="{% url project_delete project.slug %}" method="POST" style="display: none;">
                             <input type="submit" value="{% trans "Delete Project" %}" /> (all content will be removed)
                        </form>
                    {% else %}
                        <p>You are member of this project.</p>
                    {% endifequal %}
                {% else %}
                    <p>It is up to the creator of this project (<a href="{% url profiles.views.profile project.creator.username %}">{{ project.creator }}</a>) to add you.</p>
                {% endif %}
            </div>
        {% endif %}

        {% if user.is_authenticated %}

            <h2>Tasks{% if total_tasks %} ({{ total_tasks }}){% endif %}</h2>

            <p><a href="{% url project_tasks project.slug %}" method="Post">view more details or add task...</a></p>

            <table class="tasks">
                {% for task in tasks %}
                    <tr class="task_{{ task.get_state_display }} {% cycle odd,even %}">
                        <td><a href="{% url project_task task.id %}">{{ task.summary }}</a></td>
                        <td>{{ task.modified|shorttimesince }} ago</td>
                        <td>
                            {% if task.assignee %}
                                {{ task.assignee }}
                            {% else %}
                                <span class="warning">unassigned</span>
                            {% endif %}
                        </td>
                        <td>{{ task.status }}</td>
                    </tr>
                {% endfor %}
            </table>

            <h2>Discussion Topics{% if project.topics.all.count %} ({{ project.topics.all.count }}){% endif %}</h2>

            {% if topics %}
                <p><a href="{% url project_topics project.slug %}">view more details or start new topic...</a></p>

                {% for topic in topics %}
                    {% show_project_topic topic %}
                {% endfor %}
            {% else %}
                <p>None yet. <a href="{% url project_topics project.slug %}">Start one...</a></p>
            {% endif %}


            <h2>Wiki Articles{% if total_articles %} ({{ total_articles }}){% endif %}</h2>

            {% if articles %}
                <table class="topics">
                    {% for article in articles %}
                        {% show_teaser article %}
                    {% endfor %}
                </table>
                <p><a href="{% wikiurl list project %}">more...</a></p>
            {% else %}
                {% wikiurl list project as wiki_url %}
                <p>
                    {% blocktrans %}
                        None yet. Go to the <a href="{{ wiki_url }}" title="wiki">wiki</a> to start a new article.
                    {% endblocktrans %}
                </p>
            {% endif %}

            {% if photos %}
                <h2>Project Photo Pool</h2>
                <div class="thumb-row clearfix">
                    {% for photo in photos %}
                    <div class="gallery-photo-thumb">
                        <a href="/photos/details/{{ photo.photo.id }}/"><img src="{{ photo.photo.get_thumbnail_url }}" alt="{{ photo.photo.title }}"/></a><br />
                        <img src="/site_media/comment.png" border="0" class="noborder" align="absmiddle" /><a href="/photos/details/{{ photo.photo.id }}/">{% get_comment_count for photo.photo as commentcount %} {{ commentcount }} Comments</a>
                   </div>
                   {% endfor %}
               </div>
            {% endif %}

            {% comment %}
                <h2>{% trans "Tweets" %}</h2>

                {% tweet_listing tweets 1 0 %}
            {% endcomment %}

        {% endif %}
    </div>

{% endblock %}

{% block extra_body %}
    {{ block.super }}
    <script type="text/javascript" src="/site_media/jquery.bgiframe.js"></script>
    <script type="text/javascript" src="/site_media/jquery.dimensions.js"></script>
    <script type="text/javascript" src="/site_media/jquery.ajaxQueue.js"></script>
    <script type="text/javascript" src="/site_media/jquery.autocomplete.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#id_recipient").autocomplete("{% url profile_username_autocomplete %}", {
                formatItem: function(data, i, n, value) {
                    values = value.split(",,");
                    return values[0] + values[1] + "<br />" + values[2];
                },
                formatResult: function(data, value) {
                    return value.split(",,")[1];
                },
                // TODO: improve highlighting to only affect username.
                highlight: false,
            });
            $('#edit-project').hide();
            $('#edit-project-toggle').click(function() {
                $('#edit-project').slideToggle();
                $('#edit-project').autoscroll();
                return false;
            });
            if ($('#edit-project ul.errorlist').length) {
                $('#edit-project').show();
                $('#edit-project ul.errorlist').autoscroll();
            }
            $('#add-member').hide();
            $('#add-member-toggle').click(function() {
                $('#add-member').slideToggle();
                $('#add-member').autoscroll();
                return false;
            });
            if ($('#add-member ul.errorlist').length) {
                $('#add-member').show();
                $('#add-member ul.errorlist').autoscroll();
            }
        });
    </script>
{% endblock %}
